package cn.lyf.myblog.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.lyf.myblog.mybatis.mappers.LabelBlogMapper;
import cn.lyf.myblog.mybatis.mappers.LabelMapper;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.*;


import static cn.lyf.myblog.controller.api.JsonUtils.*;

import java.util.List;


@Service
public class LabelService {
    @Autowired
    LabelBlogMapper labelBlogMapper;
    @Autowired
    LabelMapper labelMapper;
    @Autowired
    BlogService blogService;

    public JsonNode getBlogs(int lid, int uid) {
        ObjectNode res = mapper.createObjectNode();
        res.put("success", true);
        res.put("code", 200);
        res.put("message", "获取标签博客成功！");
        res.putPOJO("result",  labelBlogMapper.selectBidByLidAndUid(lid, uid).stream().map(blogService::getDetailedBlogMap));
        return res;
    }

    public JsonNode getLabels() {
        ObjectNode res = mapper.createObjectNode();
        res.put("success", true);
        res.put("code", 200);
        res.put("message", "获取标签成功！");
        ObjectNode res2 = mapper.createObjectNode();
        res.set("result", res2);
        List<?>l=labelMapper.selectAll();
        res2.putPOJO("list",l);
        res2.put("total",l.size());        
        return res;
    }
}
